package com.warmlight.voicepacket.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 文字变色工具类
 */
public class StringUtils {



        /**
         * 关键字高亮变色
         *
         * @param color
         *            变化的色值
         * @param text
         *            文字
         * @param keyword
         *            文字中的关键字
         * @return 结果SpannableString
         */
        public static SpannableString changeTextColor(int color, String text, String keyword) {
            SpannableString s = new SpannableString(text);
            keyword=escapeExprSpecialWord(keyword);
            text=escapeExprSpecialWord(text);
            if (text.contains(keyword)&&!TextUtils.isEmpty(keyword)){
                try {
                    Pattern p = Pattern.compile(keyword);
                    Matcher m = p.matcher(s);
                    while (m.find()) {
                        int start = m.start();
                        int end = m.end();
                        s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }catch (Exception e){
//                    AppLog.e(e.toString());
                }
            }
            return s;
        }

        /**
         * 转义正则特殊字符 （$()*+.[]?\^{},|）
         *
         * @param keyword
         * @return keyword
         */
        public static String escapeExprSpecialWord(String keyword) {
            if (!TextUtils.isEmpty(keyword)) {
                String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };
                for (String key : fbsArr) {
                    if (keyword.contains(key)) {
                        keyword = keyword.replace(key, "\\" + key);
                    }
                }
            }
            return keyword;
        }




    /**
     *   关键字高亮显示
     *
     *   @param text 文字
     *
     *   @param keyword1 文字中的关键字数组
     *
     *   @return
     *
     */
    public static SpannableStringBuilder changeTextColor(int color,String text, String[] keyword1) {
        String[] keyword = new String[keyword1.length];
        System.arraycopy(keyword1, 0, keyword, 0, keyword1.length);
        SpannableStringBuilder spannable = new SpannableStringBuilder(text);

        CharacterStyle span;
        String wordReg;
        for (int i = 0; i < keyword.length; i++) {
            String key = "";
            //  处理通配符问题
            if (keyword[i].contains("*") || keyword[i].contains("(") || keyword[i].contains(")")) {
                char[] chars = keyword[i].toCharArray();
                for (int k = 0; k < chars.length; k++) {
                    if (chars[k] == '*' || chars[k] == '(' || chars[k] == ')') {
                        key = key + "\\" + String.valueOf(chars[k]);
                    } else {
                        key = key + String.valueOf(chars[k]);
                    }
                }
                keyword[i] = key;
            }

            wordReg = "(?i)" + keyword[i];   //忽略字母大小写
            Pattern pattern = Pattern.compile(wordReg);
            Matcher matcher = pattern.matcher(text);
            while (matcher.find()) {
                span = new ForegroundColorSpan(color);
                spannable.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK);
            }
        }

        return spannable;
    }

    /**
     * 判断手机号是否有效
     * @param phoneNum 手机号
     * @return 有效则返回true, 无效则返回false
     * */
    public static boolean isPhoneNumValid(String phoneNum) {
//        return phoneNum.length() == 11 && phoneNum.matches("[0-9]{1,}");
        Pattern pattern = Pattern.compile("^1[0|3|4|5|7|8][0-9]\\d{8}$");
        Matcher matcher = pattern.matcher(phoneNum);
        return matcher.matches();
    }

    public static boolean isMobilePhoneNumber(String number) {
        Pattern pattern = Pattern.compile("^1[0|3|4|5|7|8][0-9]\\d{8}$");
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }
    //密码6-16位正则表达式
    public static boolean isPasswordNumber(String number) {
        Pattern pattern = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,16}$");
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }

    public static String spliteLable(String s) {
        StringBuffer buffer = new StringBuffer(s);
        int index;
        for (index = 1; index < buffer.length(); index += 2) {
            buffer.insert(index, '\n');
        }
        return  buffer.toString();
    }

    /**
     *
     * @param password 用户输入密码
     * @return 有效则返回true, 无效则返回false
     */
    public static boolean isPasswordValid(String password) {
        return password.length() >= 6 && password.length() <= 12;
    }
}
